<?php
/*
Nguyen Viet Manh
manhnv@vietnambiz.com
*/

//select data on table note with 5 lastest row
function listNote(){
	 global $db,$CONFIG;
	 $rsNote =$db->query("SELECT * FROM note WHERE id_mem=".intval($_SESSION['adlogin']["id"])." ORDER BY date desc LIMIT 0,5");
	 return $rsNote;
}
//select data on table note with id
function listNoteId(){
	global $db,$CONFIG;
	$rs =$db->query_first("SELECT * FROM note WHERE id=".intval($_GET['id']));
	return $rs;
}
//select data on table note with id_mem
function listNoteAll(){
	global $db,$CONFIG;
	if(intval($_REQUEST['page'])>0) {
        $page = $_REQUEST['page'];
    }else {
        $page = 1;
    }
	$rsNote =$db->query("SELECT id FROM note WHERE id_mem=".intval($_SESSION['adlogin']['id'])."");
	$totalRows= $db->num_rows($rsNote);
	$totalPages = ceil($totalRows/$CONFIG ['rows_per_page_news']);
	$start = (($page*$CONFIG ['rows_per_page_news'])-$CONFIG ['rows_per_page_news']);
    $limit = $CONFIG ['rows_per_page_news'];
    $rs = $db->query("SELECT * FROM note WHERE id_mem=".intval($_SESSION['adlogin']['id'])." ORDER BY date desc LIMIT $start,$limit");
    if($db->num_rows($rs)<1) {
        $page = $page - 1;
        if($page>1) {
            server_redirect(get_string_url()."&page=".$page);
        }
    }
    return array(
    "rs"=>$rs,
    "page"=>$page,
    "total"=>$totalPages
    );
}
//add data on table note
function addNote(){
	global $db,$CONFIG;
	if(isset($_POST['submitFrmNoteAdd'])){
		$data = array();
		if($_POST['message']==''){
			$CONFIG['error_message'].='<li>Bạn chưa nhập ghi chú!</li>';
		}
		$data['message']=$_POST['message'];
		$data['date']=date('Y-m-d H:i:s');
		$data['id_mem']=$_SESSION['adlogin']['id'];
		if($CONFIG['error_message']==''){
			unset($_REQUEST);
			$db->exec_insert('note',$data);
			if($_POST['submitType']=='exit') {
            	$url =$_SERVER['PHP_SELF'].'?module=note';
                echo "<script>window.parent.location.href='".$url."';window.parent.tb_remove();</script>";
            }
            $CONFIG["success_message"] = "<li>thêm mới ghi chú thành công!</li>";
		}
	}
}
//edit data on table note with id
function editNote(){
	global $db,$CONFIG;
	if(isset($_POST['submitFrmNoteEdit'])){
		$data=array();
		if($_POST['message']==''){
			$CONFIG['error_message'].='<li>Bạn chưa nhập tin nhắn!</li>';
		}
		$data['message']=$_POST['message'];
		$id=intval($_GET['id']);
		if($CONFIG['error_message']==''){
			unset($_REQUEST);
			$db->exec_update('note',$data,'id='.$id);
			if($_POST['submitType']=='exit') {
            //server_redirect(ADMIN_URL . '?module=news');
            	$url =$_SERVER['PHP_SELF'].'?module=note';
                echo "<script>window.parent.location.href='".$url."';window.parent.tb_remove();</script>";
                //echo "ok";
            }
            $CONFIG["success_message"] = "<li>Cập nhật ghi chú thành công!</li>";
		}
		
	}
}
//delete data on table note with id
function delNote(){
	global $db;
	$id = intval($_REQUEST['id']);
    if($id>0) {
        $db->exec_delete('note',"id=$id");
    }
}
//delete data on table note with multi id
function delMultiNote(){
	global $db;
	if($_POST['del_multi']=='okie'){
		$arrNoteId = $_POST['checkNote'];
		for($i=0;$i<count($arrNoteId);$i++) {
	        if(intval($arrNoteId[$i])>0) {
	            $db->exec_delete('note','id='.intval($arrNoteId[$i]));
	        }
	    }
    }
	
}
//
function split_char($str, $limit = 50, $opt = 0) {
	$string = mb_substr($str,0, $limit, 'UTF-8');
	if($opt == 1) { // Ki?u c?t th�m
	  $tempLen = mb_strlen($string);
	  for($i = $limit; $i < $tempLen; $i++) {
	   $str_tmp = mb_substr($str, $i,1, 'UTF-8');
	   if($str_tmp != ' ') {
		$string .= $str_tmp;
	   }
	   else { break; die(); }
	  }
	}
	elseif($opt == -1) { //Ki?u c?t b?t
	  for($i = $limit; $i > 2; $i--) {
	   $str_tmp = mb_substr($str, $i, 1, 'UTF-8');
	   if($str_tmp != ' ') {
		$string = mb_substr($string,0, -1, 'UTF-8');
	   }
	   else {break;}
	  }
	}

	return $string;
}
?>